#!/usr/bin/env python312 
# -*- coding: utf-8 -*-
#-------------------------------------------------------
#	FileName	: var.py
#	Author		：hpy
#	Date		：2024年03月02日
#	Description	：计算最大偏差
#-------------------------------------------------------
from sys import argv
import numpy as np
import math 
import matplotlib.pyplot as plt
import sys
import argparse
#-------------------------------------------------------
#	从txt文件加载光谱数据
#   文件格式：第一列x  第二列y
#-------------------------------------------------------
def loadreadtxt(file_name):
    data = []
    file = open(file_name,'r')  #打开文件
    file_data = file.readlines() #读取所有行
    for row in file_data:
        tmp_list = row.split(' ') #按‘，’切分每行的数据
        tmp_list[-1] = tmp_list[-1].replace('\n','') #去掉换行符 \n
        # print(tmp_list)
        data.append(tmp_list) #将每行数据插入data中

    return data 

# 绘图滚动缩放
def call_back(event):
    axtemp=event.inaxes
    x_min, x_max = axtemp.get_xlim()
    fanwei = (x_max - x_min) / 10
    if event.button == 'up':
        axtemp.set(xlim=(x_min + fanwei, x_max - fanwei))
        # print('up')
    elif event.button == 'down':
        axtemp.set(xlim=(x_min - fanwei, x_max + fanwei))
        # print('down')
    fig.canvas.draw_idle()  # 绘图动作实时反映在图像上



if __name__=="__main__":
    # 参数解析 
    # 创建解析步骤
    parser = argparse.ArgumentParser(description='FTIR Resample')
    parser.add_argument("file", type = str, help = "filename")
    parser.add_argument('-x',type=str, dest="x", default="",help='set x label')
    parser.add_argument('-y',type=str, dest="y", default="",help='set y label')
    parser.add_argument('-t',type=str, dest="title", default="",help='set title')
    # 解析参数步骤  
    args = parser.parse_args()
    #

    data = np.loadtxt(args.file)
    # print(data.shape[0])
    # print(data.shape[1])
    fig = plt.figure()
    # fig.canvas.mpl_connect('scroll_event', call_back)
    # fig.canvas.mpl_connect('button_press_event', call_back)

    if (len(args.title) >0 ) :
        plt.title(args.title)
    if (len(args.x) >0 ) :
        plt.xlabel(args.x)  
    if (len(args.y) >0 ) :       
        plt.ylabel(args.y) 

    for col in range(1,data.shape[1],1):
        plt.plot(data[:,0], data[:,col] )

    plt.show()
